<?php

namespace app\models;

use Yii;
use common\models\BaseImg;

/**
 * This is the model class for table "{{%base_goods}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $name_en
 * @property integer $type_id
 * @property integer $brand_id
 * @property string $unit
 * @property string $market_price
 * @property string $content
 * @property string $description
 * @property integer $status
 * @property integer $is_deleted
 * @property integer $img_id
 * @property string $keywords
 * @property string $tag
 * @property string $code
 * @property string $national
 * @property string $expiration
 * @property string $standards
 * @property string $pzcode
 * @property integer $admin_id
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $user_id
 * @property integer $update_admin_id
 * @property integer $purchase
 * @property integer $supplier
 * @property string $listed_time
 * @property integer $statuses
 */
class BaseGoods extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%base_goods}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'type_id', 'brand_id'], 'required'],
            [['type_id', 'brand_id', 'status', 'is_deleted', 'img_id', 'admin_id', 'created_at', 'updated_at', 'user_id', 'update_admin_id', 'purchase', 'supplier', 'statuses','salse'], 'integer'],
            [['market_price'], 'number'],
            [['content'], 'string'],
            [['name', 'name_en', 'code'], 'string', 'max' => 128],
            [['unit'], 'string', 'max' => 8],
            [['description', 'keywords', 'tag', 'national', 'expiration'], 'string', 'max' => 255],
            [['standards', 'pzcode', 'listed_time'], 'string', 'max' => 150]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '编号',
            'name' => '商品名称',
            'name_en' => 'Name En',
            'type_id' => '商品类型ID',
            'brand_id' => '商品品牌ID',
            'unit' => '商品单位',
            'market_price' => '市场价',
            'content' => '商品介绍',
            'description' => '商品备注',
            'status' => '商品状态 -1：禁用；0：未审核；1：已审核',
            'is_deleted' => '是否删除',
            'img_id' => '封面ID',
            'keywords' => '搜索标签',
            'tag' => 'Tag',
            'code' => '条形码',
            'national' => '原产国',
            'expiration' => '保质期',
            'standards' => '规格',
            'pzcode' => '批准文号',
            'admin_id' => '管理员ID',
            'created_at' => '添加时间',
            'updated_at' => '更新时间',
            'user_id' => '用户id',
            'update_admin_id' => '更新操作员',
            'purchase' => '求购个数',
            'supplier' => '货源个数',
            'listed_time' => '上市时间',
            'statuses' => '0下架，1上架',
        ];
    }
       static function getGoodsByspm($spm){
        return static::findOne(['name' => $spm]);
    }
    public function getGoodsImg(){
        return $this->hasOne(BaseImg::className(),['id'=>'img_id']);
    }
    /**

     * author:孙健  
     * create_time：2016年1月25日10:07:30
     * getImgByGoodId 通过商品id获取图片
     *params ：goodsid
     * return：string（img_url） 
     *      */
    
    static function getImgByGoodID($goodsid){
        $good=  BaseGoods::find()->where(['dh_base_goods.id' => $goodsid])->joinWith('goodsImg')->asArray()->one();
        return IMAGE_URL.$good['goodsImg']['img_url'];
    }
    
}
